package com.wenhui.schoolyardinfo.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnLongItemViewClickListener;
import com.wenhui.schoolyardinfo.firstpage.bean.PhotoBean;
import com.wenhui.schoolyardinfo.manager.LogManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/6 11:34
 * introduce :
 */

public class AlbumAdapter extends RecyclerView.Adapter {

    private static final String TAG = "AlbumAdapter";
    private Context context;
    private List<PhotoBean> photoBeanList = new ArrayList<>();
    private boolean isEditAlbum = false;

    public AlbumAdapter(Context context) {
        this.context = context;
    }

    public AlbumAdapter(Context context, List<PhotoBean> photoBeanList) {
        this.context = context;
        this.photoBeanList = photoBeanList;
    }

    public AlbumAdapter(Context context, List<PhotoBean> photoBeanList, boolean isEditAlbum) {
        this.context = context;
        this.photoBeanList = photoBeanList;
        this.isEditAlbum = isEditAlbum;
    }

    public void clearData() {
        photoBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<PhotoBean> photoBeanList) {
        this.photoBeanList.addAll(photoBeanList);
        notifyDataSetChanged();
    }

    public void addAllData(List<PhotoBean> photoBeanList, boolean isEditAlbum) {
        this.photoBeanList.addAll(photoBeanList);
        this.isEditAlbum = isEditAlbum;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.item_album_content, parent, false);
        return new ContentHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) {
            LogManager.i(TAG, "ContentHolder*****" + position);
            ContentHolder contentHolder = (ContentHolder) holder;
            Glide.with(context).load(photoBeanList.get(position).getPhotoUrl()).into(contentHolder.imvPhoto);
            if (isEditAlbum) {
                contentHolder.layoutEditAlbum.setVisibility(View.VISIBLE);
            } else {
                contentHolder.layoutEditAlbum.setVisibility(View.GONE);
            }
            if (photoBeanList.get(position).isChecked()) {
                contentHolder.imvEditAlbum.setImageResource(R.mipmap.icon_edit_album_blue);
            } else {
                contentHolder.imvEditAlbum.setImageResource(R.mipmap.icon_edit_album_gray);
            }
        }
    }

    @Override
    public int getItemCount() {
        return photoBeanList.size();
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        @BindView(R.id.imv_photo)
        ImageView imvPhoto;
        @BindView(R.id.imv_edit_album)
        ImageView imvEditAlbum;
        @BindView(R.id.layout_edit_album)
        RelativeLayout layoutEditAlbum;
        @BindView(R.id.layout_photo)
        RelativeLayout layoutPhoto;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            imvPhoto.setOnClickListener(this);
            imvPhoto.setOnLongClickListener(this);
            layoutEditAlbum.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.imv_photo:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_edit_album:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }

        @Override
        public boolean onLongClick(View view) {
            switch (view.getId()) {
                case R.id.imv_photo:
                    rcvOnLongItemViewClickListener.onLongItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }

    private RcvOnLongItemViewClickListener rcvOnLongItemViewClickListener;

    public void setRcvOnLongItemViewClickListener(RcvOnLongItemViewClickListener rcvOnLongItemViewClickListener) {
        this.rcvOnLongItemViewClickListener = rcvOnLongItemViewClickListener;
    }
}
